use ${newdata}carownership_dataset_bergen, clear

/* Only keep required obs */
keep if $trmgroup != 0 
keep if $trmgroup != .
keep if 	(year >= $firstyearpre  & year <= $lastyearpre) ///
		  | (year >= $firstyearpost & year <= $lastyearpost)

/* setup variables */
capt drop bergen 		  
gen commuters = ($trmgroup == 1 | $trmgroup == 3) 
gen bergen = 	($trmgroup == 1 | $trmgroup == 2)
gen post = (year >= $firstyearpost & year <= $lastyearpost)
gen bergen_commuters = bergen * commuters
gen commuters_post = commuters * post
gen bergen_commuters_post = bergen_commuters * post

/* =============================================================================
Only keeping 2014 and 2017 !!!
 =============================================================================*/
*keep if year == 2011 | year == $lastyearpre | year == $lastyearpost 
 
 
sort familienr year
egen long famid_num = group(familienr)


/* Import list of neighborhoods inside the toll cordon */

preserve 	
	import delimited ${additionaldata}bergen_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile bergencenter
	save `bergencenter', replace	
	rename grk grk_work1
	tempfile bergencenter_w1
	save `bergencenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile bergencenter_w2
	save `bergencenter_w2', replace
	
* Stavanger, Haugesund, Kristiansand	
	import delimited ${additionaldata}stavanger_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile stavangercenter
	save `stavangercenter', replace
	rename grk grk_work1
	tempfile stavangercenter_w1
	save `stavangercenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile stavangercenter_w2
	save `stavangercenter_w2', replace
	
	import delimited ${additionaldata}haugesund_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile haugesundcenter
	save `haugesundcenter', replace
	rename grk grk_work1
	tempfile haugesundcenter_w1
	save `haugesundcenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile haugesundcenter_w2
	save `haugesundcenter_w2', replace
	
	import delimited ${additionaldata}kristiansand_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile kristiansandcenter
	save `kristiansandcenter', replace	
	rename grk grk_work1
	tempfile kristiansandcenter_w1
	save `kristiansandcenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile kristiansandcenter_w2
	save `kristiansandcenter_w2', replace	
restore	

capt drop insidetc 
capt drop aux

gen insidetc = 0 
gen insidetc_w1 = 0
gen insidetc_w2 = 0
* Living inside toll cordon
merge m:1 grk using `bergencenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge 
merge m:1 grk using `stavangercenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge
merge m:1 grk using `haugesundcenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge
merge m:1 grk using `kristiansandcenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge
* Working inside toll cordon
merge m:1 grk_work1 using `bergencenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge
merge m:1 grk_work1 using `stavangercenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge
merge m:1 grk_work1 using `haugesundcenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge
merge m:1 grk_work1 using `kristiansandcenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge

merge m:1 grk_work2 using `bergencenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge
merge m:1 grk_work2 using `stavangercenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge
merge m:1 grk_work2 using `haugesundcenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge
merge m:1 grk_work2 using `kristiansandcenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge

gen aux = insidetc 
replace aux = . if year != $lastyearpre
bys famid_num: egen inside_2014 = mean(aux)
drop aux

gen aux = insidetc_w1 
replace aux = . if year != $lastyearpre
bys famid_num: egen inside_w1_2014 = mean(aux)
drop aux

gen aux = insidetc_w2 
replace aux = . if year != $lastyearpre 
bys famid_num: egen inside_w2_2014 = mean(aux)
drop aux

* Making indicators for outside-in - inside-out and outside-outside ===

gen commuter_type = 0
*  living inside, working outside
replace commuter_type = 1 if insidetc == 1 ///
	& (insidetc_w1 == 0 | insidetc_w1 == .) ///
	& (insidetc_w2 == 0 | insidetc_w2 == .)
* Living and working inside
replace commuter_type = 2 if insidetc == 1 & (insidetc_w1 == 1 | insidetc_w2 == 1)	
* working inside, living outside
replace commuter_type = 3 if insidetc == 0 & (insidetc_w1 == 1 | insidetc_w2 == 1)	
* working outside, living outside 
replace commuter_type = 4 if insidetc == 0 & (insidetc_w1 == 0 & insidetc_w2 == 0)

tab commuter_type if year == $lastyearpre 
tab commuter_type group if year == $lastyearpre

* ===
	
gen workdistance = dist

replace grk_work1 = 0 if grk_work1 == .
replace grk_work2 = 0 if grk_work2 == .

	
* All time-varying variables based on geography (residence or work location)
foreach var in dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean grk {
	replace `var' = . if year != 2014
	bysort familienr (`var'): replace `var' = `var'[_n-1] if missing(`var')
}
	
	
*-------------------------------------------------------------------------------	
* Regressions
*-------------------------------------------------------------------------------	
* Base regression
reghdfe bev ///
	bergen_commuters_post /// DiDiD: post commuter in bergen		
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	, absorb( ///
		i.grk#i.year /// neighborhood times year
		i.famid_num /// household fe
	) vce(cluster grk)	

	eststo reg_1
	summarize bev if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize bev if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_1	
	estimates save ${ster}tableD3_1, replace	
	
	
* Excluding people living inside	
reghdfe bev ///
	bergen_commuters_post /// DiDiD: post commuter in bergen		
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	if commuter_type == 3 | commuter_type == 4, absorb( ///
		i.grk#i.year /// neighborhood times year
		i.famid_num /// household fe
	) vce(cluster grk)	
	
	eststo reg_2
	summarize bev if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize bev if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_2	
	estimates save ${ster}tableD3_2, replace
	
* Only people living inside 
reghdfe bev ///
	bergen_commuters_post /// DiDiD: post commuter in bergen		
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	if commuter_type == 1 | commuter_type == 2, absorb( ///
		i.grk#i.year /// neighborhood times year
		i.famid_num /// household fe
	) vce(cluster grk)	

	eststo reg_3
	summarize bev if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize bev if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_3	
	estimates save ${ster}tableD3_3, replace
	
	
gen paying_transit = 0
replace paying_transit = 1 if commuter_type == 4 & (group == 1 | group == 3)	
* Excluding paying commuters passing through the toll cordon
reghdfe bev ///
	bergen_commuters_post /// DiDiD: post commuter in bergen		
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	if paying_transit == 0 , absorb( ///
		i.grk#i.year /// neighborhood times year
		i.famid_num /// household fe
	) vce(cluster grk)
	
	eststo reg_4
	summarize bev if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize bev if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_4	
	estimates save ${ster}tableD3_4, replace
	
* Only pass-through:
reghdfe bev ///
	bergen_commuters_post /// DiDiD: post commuter in bergen		
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	if commuter_type == 4 , absorb( ///
		i.grk#i.year /// neighborhood times year
		i.famid_num /// household fe
	) vce(cluster grk)	
	
	eststo reg_5
	summarize bev if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
	estadd scalar MeanValue1 = r(mean), replace
	summarize bev if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
	estadd scalar MeanValue2 = r(mean), replace
	eststo reg_5	
	estimates save ${ster}tableD3_5, replace
	
	
/* === Read estimates from ster, write table to tex ========================= */
eststo drop *

forvalues i = 1/5 {
	estimates use "${ster}tableD3_`i'" 
	eststo
}
esttab * ///
	using "${tables}tabD3_est.tex", ///
	keep(bergen_commuters_post)  ///
	coeflabels( ///
	bergen_commuters_post "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

esttab * ///
	using "${tables}tabD3_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(N  MeanValue1 MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)	

* Visual	
esttab *, keep(bergen_commuters_post) stats(MeanValue1 MeanValue2 N N_clust, ///
	label("Mean depvar $reg1lastyearpre" ///
			"Mean depvar $reg1lastyearpost" ///
			"Obs" "No clusters") ///
		fmt(4 4 0 0)) ///
	star(* 0.10 ** 0.05 *** 0.01)	